<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item
              label="工程量名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="gridName"
            >
              <a-input v-model="model.gridName" @change='gridNameChange' placeholder="请输入工程量名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="工程量编码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="gridName"
            >
              <a-input v-model="model.gridCode" placeholder="请输入工程量编码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="输入方式"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="gridInputType"
            >
              <!-- <a-select v-model="model.gridInputType" placeholder="请选择" @change="changeGridInputType">
                <a-select-option value="INPUT">手输 </a-select-option>
                <a-select-option value="SELECT">选择 </a-select-option>
                <a-select-option value="BYTE_STREAM">字节流 </a-select-option>
                <a-select-option value="BYTE_STREAM_GROUP">字节流组 </a-select-option>
              </a-select>-->

              <j-dict-select-tag
                v-model="model.gridInputType"
                dictCode="grid_input_type"
                placeholder="请选择"
                @change="changeGridInputType"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              v-if="model.gridInputType === 'SELECT'"
              label="枚举"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="gridEnumsTypeId"
            >
              <a-select
                v-model="model.gridEnumsTypeId"
                placeholder="请选择"
                show-search
                optionFilterProp="children"
              >
                <a-select-option :value="item.id" v-for="item in GridEnumList" :key="item.id">
                  {{
                  item.enumTypeName
                  }}
                </a-select-option>
              </a-select>
              <!-- <a-input v-model="model.gridEnumsTypeId" placeholder="请输入当input_type为enums对应枚举id"></a-input> -->
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="包长度类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gridLengthType">
              <a-select v-model="model.gridLengthType" placeholder="请选择">
                <a-select-option value="STATIC">定长 </a-select-option>
                <a-select-option value="DYNAMICS">动态长度 </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-model-item
              v-if="model.gridInputType ==='UNKNOW'|| model.gridInputType === 'TIME_STAMP' ||model.gridInputType === 'INPUT' || model.gridInputType === 'BYTE_STREAM'"
              label="定长包长度"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="gridStaticLength"
            >
              <a-input-number
                :min="0"
                defaultValue="1"
                v-model="model.gridStaticLength"
                placeholder="请输入定长包长度"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              v-if="model.gridInputType === 'INPUT'"
              label="分辨率"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input-number
                v-model="model.resolvingPower"
                placeholder="请输入分辨率"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              v-if="model.gridInputType === 'BYTE_STREAM' || model.gridInputType === 'BYTE_STREAM_GROUP'"
              label="指定的工程量"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="gridDynamicsLengthId"
            >
              <a-select
                v-model="model.gridDynamicsLengthId"
                placeholder="请选择"
                show-search
                optionFilterProp="children"
              >
                <a-select-option
                  :value="item.id"
                  v-for="item in gridDynamicsLengthIdList"
                  :key="item.id"
                >
                  {{
                  item.gridName
                  }}
                </a-select-option>
              </a-select>
              <!-- <a-input v-model="model.gridDynamicsLengthId" placeholder="请输入动态包长度 指定的工程量id"></a-input> -->
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'NmcPackageGridForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {
        // 输入方式
        gridInputType: 'INPUT',
        // 定长包长度
        gridStaticLength: 2
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        PackageGridList: '/grid/nmcPackageGrid/list',
        GridEnumList: '/grid/nmcPackageGridEnumType/list',
        add: '/grid/nmcPackageGrid/add',
        edit: '/grid/nmcPackageGrid/edit',
        queryById: '/grid/nmcPackageGrid/queryById',
      },
      gridDynamicsLengthIdList: [],
      GridEnumList: [],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    /**
     * 工程量名称变换后直接赋值工程量编码
     * @param val
     */
    gridNameChange(val) {
      this.model.gridCode = this.model.gridName;
    },
    getList() {
      getAction(this.url.PackageGridList, { pageSize: -1 }).then((res) => {
        console.log(res)
        this.gridDynamicsLengthIdList = [...res.result.records]
      })
      getAction(this.url.GridEnumList, { pageSize: -1 }).then((res) => {
        console.log(res)
        this.GridEnumList = [...res.result.records]
      })
    },
    changeGridInputType(value) {
      if (value == 'INPUT') {
        this.model.gridLengthType = 'STATIC'
      } else if (value == 'SELECT') {
        this.model.gridLengthType = 'DYNAMICS'
      }
      else if (value == 'UNKNOW') {
        this.model.gridStaticLength = 0
      }
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.getList()
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
